<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        show-icon
        :closable="false"
      >
      </el-alert>
      选择商品分类：<el-cascader
        v-model="selectIDs"
        :options="categories"
        :props="{
          value: 'cat_id',
          label: 'cat_name',
          children: 'children',
          expandTrigger: 'hover',
        }"
        @change="handleChange"
      ></el-cascader>
      <el-tabs
        type="border-card"
        v-model="activetabs"
        @tab-click="handleTabClick"
      >
        <el-tab-pane label="动态参数" name="many">        
          <ctable :activetabs="activetabs" :selectIDs="selectIDs" :addname="'添加参数'" :tableData="tableData"></ctable>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <ctable :activetabs="activetabs" :selectIDs="selectIDs" :addname="'添加属性'" :tableData="tableData"></ctable>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import ctable from './ClassTable.vue'
export default {
  data() {
    return {
      selectIDs: [],
      categories: [],
      activetabs: "many",
      cateId: [],
      manyTableData:[],
      onlyTableData:[],
      tableData:[]
    };
  },
  created() {
    this.getCategs();
  },
  methods: {
    async getCategs() {
      const { data: res } = await this.$http.get("categories");
      if (res.meta.status !== 200) {
        return (this.$message.erro = "获取商品分类失败");
      }
      this.categories = res.data;
    },
    handleChange(value) {
      console.log(value);
      if (this.selectIDs.length != 3) {
        this.selectIDs = [];
        this.$message.erro = "请选择三级分类";
        return;
      }
      this.getCla();
    },
    async getCla(){
       const { data: res } = await this.$http.get(
        `categories/${this.isSelectID}/attributes`,
        { params: { sel: this.activetabs } }
      );
      if (res.meta.status !== 200) {
        return (this.$message.erro = "获取商品分类失败");
      }
      this.tableData = res.data;
    },
    handleTabClick() {
      console.log(this.activetabs);
      this.getCla();
    },
  },
  computed: {
    isSelectID() {
      if (this.selectIDs.length != 3) {
        return null;
      }
      return this.selectIDs[2];
    },
  },
  components: {
    ctable
  }
};
</script>
<style scoped>
.el-cascader {
  margin: 20px 0;
}

</style>